package com.cskaoyan.service;

import com.cskaoyan.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author stone
 * @date 2022/12/21 15:12
 */
@Transactional
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    AccountMapper accountMapper;

    //@Transactional(timeout = 5) // 超时时间为5s
    //@Transactional(timeout = 5,readOnly = true)//只读只能做查询，不能做增删改操作
    @Transactional(noRollbackFor = ArithmeticException.class) // 算术异常不回滚 → 导致30行的代码提交
    @Override
    public int transfer(int fromId, int destId, int money) {
        int fromMoney = accountMapper.selectMoney(fromId);// - money;
        int destMoney = accountMapper.selectMoney(destId);// + money;
        fromMoney -= money;
        destMoney += money;

        accountMapper.updateMoney(fromId, fromMoney);
        int i = 1 / 0;
        accountMapper.updateMoney(destId, destMoney);
        return 0;
    }

    @Override
    public int modify(int id, int money) {
        int i = accountMapper.updateMoney(id, money);
        return i;
    }


}
